﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace MyClass.PagerHelper
{
    /// <summary>
    ///  MyClass.PageHelp.MyDyPageContral myph = new MyClass.PageHelp.MyDyPageContral(ds, 4, Literal1, 2, 2, DataBinder);
    ///  PlaceHolder1.Controls.Add(myph);
    /// </summary>
    [DefaultProperty("_currentPage")]
    [ToolboxData("<{0}:MyDyPageHelper runat=server></{0}:MyDyPageHelper>")]
    public class MyDyPageContral : WebControl
    {
      
        #region 全局变量
        public delegate void DataBindEvent(PagedDataSource pds);
        private DataBindEvent _bind;
        public delegate void PageLoadEvent();
        private PageLoadEvent _load;

        public  string firstButtonImgSrc = "../images/first.gif";//图片地址
        public  string previousButtonImgSrc = "../images/previous.gif";//图片地址
        public  string nextButtonImgSrc = "../images/next.gif";//图片地址
        public  string lastButtonImgSrc = "../images/last.gif";//图片地址
        public string curCss = "curCss";//当前页样式
        public string normalCss = "normalCss";//非当前页样式
        private int _currentPage=0;
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPage { set; get; }
        private int _totalPage = 0;
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        /// <summary>
        /// 总页
        /// </summary>
        public int TotalPage { get { return _totalPage; } }
        private int _pageSize = 0;
        /// <summary>
        /// 一页的数据条数
        /// </summary>
        ///   [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int PageSize { get { return _pageSize; } }
        private int _allDataCount = 0;
        /// <summary>
        /// 总数据条数
        /// </summary>
        public int AllDataCount { get { return _allDataCount;} }
        private PagedDataSource _pds;
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        /// <summary>
        /// PagedDataSource对像
        /// </summary>
        public PagedDataSource Pds { get { return _pds; } }
        private DataSet _ds;
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        /// <summary>
        /// DataSet对像
        /// </summary>
        public DataSet Ds { get { return _ds; } }

    

        Literal _cur;
  

        ImageButton firstButton;
        ImageButton previousButton;
        ImageButton nextButton;
        ImageButton lastButton;
        LinkButton[] linBtnList;
        PlaceHolder pl;
        int _behideCount;
        int _beforeCount;
        #endregion
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="ds">数据集合</param>
        /// <param name="size">一页显示的个数</param>
        /// <param name="cur">Literal当前页,0为第一页</param>
        /// <param name="beforeCount">当前页前显示个数</param>
        /// <param name="behideCount">当前页后显示个数</param>
        /// <param name="DataBindEvent">显示数据刷新事件</param>
        public MyDyPageContral(DataSet ds, int pageSize, Literal cur, int beforeCount, int behideCount, DataBindEvent bindEvent, PageLoadEvent _load)
        {
            _behideCount = behideCount;
            _beforeCount = beforeCount;
            _bind = bindEvent;
            int count=_beforeCount+_behideCount+1;

            intital(ds,  pageSize,cur);
            CreateControls( count);
            CreateDyNav();
          
          
        }
        /// <summary>
        /// 创建控件
        /// </summary>
        /// <param name="count">控件件数</param>
        private void CreateControls(int count)
        {
            firstButton = new ImageButton();
            previousButton = new ImageButton();
            nextButton = new ImageButton();
            lastButton = new ImageButton();
            linBtnList = new LinkButton[count];

            

            if (_currentPage != 0)
            {
                firstButton.Click += new System.Web.UI.ImageClickEventHandler(firstButton_Click);
                previousButton.Click += new System.Web.UI.ImageClickEventHandler(previousButton_Click);

            }
            if (_currentPage != _totalPage - 1)
            {
                nextButton.Click += new System.Web.UI.ImageClickEventHandler(nextButton_Click);
                lastButton.Click += new System.Web.UI.ImageClickEventHandler(lastButton_Click);

            }
            for (int i = 0; i < linBtnList.Length; i++)
            {
                linBtnList[i] = new LinkButton();
                linBtnList[i].Click += new EventHandler(lb_Click);
            }
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="ds">数据集合</param>
        /// <param name="size">一页显示的个数</param>
        /// <param name="cur">当前页,0为第一页</param>
        public void intital(DataSet ds, int pageSize, Literal cur)
        {
            _pds = new PagedDataSource();
            _pds.DataSource = ds.Tables[0].DefaultView;
            _pds.AllowPaging = true;
            _pds.PageSize = pageSize;

            _currentPage = Convert.ToInt32(cur.Text) - 1;
            _totalPage = _pds.PageCount;
            _pds.CurrentPageIndex = _currentPage;
            _allDataCount = _pds.DataSourceCount;
            _pageSize = pageSize;
            _totalPage = _pds.PageCount;
            _ds = ds;
            _cur = cur;

            _bind(_pds);
  
        }
        /// <summary>
        /// 对控件附值
        /// </summary>
        public void CreateDyNav()
        {
   
       
         //  if (ph.Controls.Count == 0)
          // {
                firstButton.ImageUrl = firstButtonImgSrc;
                firstButton.Attributes.Add("alt","第一页");
                previousButton.ImageUrl = previousButtonImgSrc;
                previousButton.Attributes.Add("alt", "上一页");
                nextButton.ImageUrl = nextButtonImgSrc;
                nextButton.Attributes.Add("alt", "下一页");
                lastButton.ImageUrl = lastButtonImgSrc;
                lastButton.Attributes.Add("alt", "最后一页");



                this.Controls.Add(firstButton);
                this.Controls.Add(previousButton); //上一页
               

                //********************************************
                int navCount = _beforeCount + _behideCount + 1;//navCount 导航显示的页数
                int start = 0;//开始页数
                int count = (_currentPage - _beforeCount);//当前页与前页数差值
                int count1 = (_totalPage - _currentPage - _behideCount);//总页与前页和当前面差值
                if (count < 0)
                {
                    start = 0;

                }
                else if (count1 <= 0)
                {
                    start = _totalPage - navCount;
                    //alert(start);
                }
                else
                {
                    start = _currentPage - _beforeCount;
                }

                if (_totalPage < navCount)
                {
                    start = 0;
                    count = _totalPage;
                }
                else
                {
                    count = start + navCount;//当前实际显示的导航页数
                }

                for (int i = start; i < count; i++)
                {
                    LinkButton lb = linBtnList[i - start];
                    lb.Text = (i + 1).ToString();//生成当前页前导航
                    if (_currentPage == i)
                    {
                        lb.CssClass = curCss;
                    }
                    else
                    {
                        lb.CssClass = normalCss;
                    }

                    this.Controls.Add(lb);
                  

                }
                //********************************************


  
                this.Controls.Add(nextButton);
                this.Controls.Add(lastButton); //下一页
                
          //  }
        }
        /// <summary>
        /// 当前页改变后附值
        /// </summary>
        private void SetDyNav()
        {
            int navCount = _beforeCount + _behideCount + 1;//navCount 导航显示的页数
            int start = 0;//开始页数
            int count = (_currentPage - _beforeCount);//当前页与前页数差值
            int count1 = (_totalPage - _currentPage - _behideCount);//总页与前页和当前面差值
            if (count < 0)
            {
                start = 0;

            }
            else if (count1 <= 0)
            {
                start = _totalPage - navCount;
                //alert(start);
            }
            else
            {
                start = _currentPage - _beforeCount;
            }

            if (_totalPage < navCount)
            {
                start = 0;
                count = _totalPage;
            }
            else
            {
                count = start + navCount;//当前实际显示的导航页数
            }

            for (int i = start; i < count; i++)
            {
                LinkButton lb = linBtnList[i - start];
                lb.Text = (i + 1).ToString();//生成当前页前导航
                if (_currentPage == i)
                {
                    lb.CssClass = curCss;
                }
                else
                {
                    lb.CssClass = normalCss;
                }
            }
        }
        public void firstButton_Click(object sender, ImageClickEventArgs e)
        {
            _currentPage = 0;
          //  _page.Session["dycur"] = _currentPage;
            _cur.Text = (_currentPage + 1).ToString();
            changePage(sender, e);
           // _bind(_pds);
           
        }
        public void previousButton_Click(object sender, ImageClickEventArgs e)
        {
            if (_currentPage > 0)
            {
                _currentPage = _currentPage - 1;
            }
            _cur.Text = (_currentPage + 1).ToString();
            changePage(sender, e);
           
     
        }
        public void nextButton_Click(object sender, ImageClickEventArgs e)
        {
            if (_currentPage < _totalPage - 1)
            {
                _currentPage = _currentPage + 1;
            }
            _cur.Text = (_currentPage + 1).ToString();
            changePage(sender, e);
           
        }
        public void lastButton_Click(object sender, ImageClickEventArgs e)
        {
            _currentPage = _totalPage - 1;
            _cur.Text = (_currentPage + 1).ToString();
            changePage(sender, e);
            
        }
        public void lb_Click(object sender, EventArgs e)
        {
   
            _cur.Text = ((LinkButton)sender).Text;
            _currentPage = Convert.ToInt32(_cur.Text) - 1;

            changePage(sender, e);
            

        }

        private void changePage(object sender, EventArgs e)
        {

            int count = _beforeCount + _behideCount + 1;
            intital(_ds, _pageSize, _cur);
            SetDyNav();
            _load();
        }

    }
}
